<template>
  <!-- 绑定套餐 -->
  <div class="app-container">
    <el-form ref="form" :model="form" label-width="150px">
      <card-find v-model="form.code_zone" @getCardsCb="getCardsCb" />

      <el-form-item v-for="(item, index) in form.mealOptions" :label="`套餐` + (index + 1)" :key="index">
        <el-col :span="12" style="display: flex">
          <el-select style="width: 300px" filterable v-model="form.mealOptions[index]" placeholder="填写查找套餐">
            <el-option
              v-for="(item, index) in meals"
              :key="index"
              :label="`${item.name}/${item.call_mins}分钟/${item.flow ? item.flow + 'M' : '无限流量'}/￥${
                item.meal_price
              }`"
              :value="item.id"
            ></el-option>
          </el-select>

          <el-button
            v-if="form.mealOptions.length > 1"
            type="danger"
            @click="form.mealOptions.splice(index, 1)"
            style="margin-left: 10px"
            >-</el-button
          >
          <el-button v-if="index === 0" type="success" style="margin-left: 10px" @click="form.mealOptions.push(null)"
            >+</el-button
          >
        </el-col>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="submit">确认</el-button>
        <!-- <el-button>取消</el-button> -->
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { getList as getMealList, postBindMeal } from '@/api/meal';
import CardFind from './components/CardFind';
export default {
  components: {
    CardFind,
  },
  data() {
    return {
      form: {
        mealOptions: [null],
      },
      meals: [],
    };
  },
  created() {
    this.getMealList();
  },
  methods: {
    getCardsCb(res) {
      this.form.mealOptions = res.data.meals.map((e) => e.meal_id);
    },
    async getMealList() {
      const res = await getMealList();
      this.meals = res.data.list;
    },
    async submit() {
      await postBindMeal({ form: this.form });
      this.$message({ message: '操作成功！', type: 'success' });
      setTimeout(() => this.$router.go(-1), 500);
    },
  },
};
</script>

<style lang="scss" scoped>
.app-container {
  padding: 40px 5%;

  .line {
    text-align: center;
  }
}
</style>
